<?php
/**
 * Created by PhpStorm.
 * User: td
 * Date: 15/2/8
 * Time: 下午1:30
 */

namespace Home\Model;
use Think\Model;

class MissUserRelateModel extends Model {
    protected $tableName = 'miss_user_relate';
    protected $pk        = 'id';

    public function getReceivedMission($follower_id){

        $result = array();
        $result1 = array();
        $result2 = array();
        $result3 = array();
        //申请中的活
        $miss_user_relate_field = array('mission_id');
        $miss_user_relate_order = '`id` desc';
        $miss_user_relate_map1['follower_id'] = array('eq',$follower_id);
        $miss_user_relate_map1['is_selected'] = array('eq',1);
        $miss_user_relate_reslt1 = $this->field($miss_user_relate_field)->order($miss_user_relate_order)->where($miss_user_relate_map1)->select();
        //dump($miss_user_relate_reslt);
        if(is_array($miss_user_relate_reslt1)){
            $i = 0;
            foreach($miss_user_relate_reslt1 as &$miss_user_relate_reslt1_one){
                //获取任务详情－mission表
                $mission_field = array('mission_id','mission_name','jobs','duration','salary1','salary2','location1','location2','publish_time','status','valid_time','agent');
                $mission_map1['mission_id'] = array('eq',$miss_user_relate_reslt1_one['mission_id']);
                $mission_db = M('Mission');
                $mission_result1 = $mission_db->field($mission_field)->where($mission_map1)->select();
                if(is_array($mission_result1)){
                    $result1[$i] = $mission_result1[0];
                    $i += 1;
                }else{
                    //获取mission表信息失败
                }

            }

            $result[0] = $result1;
        }else{
            $result[0] = $result1;
        }

        //进行中的活
        $miss_user_relate_field = array('mission_id');
        $miss_user_relate_order = '`id` desc';
        $miss_user_relate_map2['follower_id'] = array('eq',$follower_id);
        $miss_user_relate_map2['is_selected'] = array('eq',2);
        $miss_user_relate_reslt2 = $this->field($miss_user_relate_field)->order($miss_user_relate_order)->where($miss_user_relate_map2)->select();
        //dump($miss_user_relate_reslt);
        if(is_array($miss_user_relate_reslt2)){
            $i = 0;
            foreach($miss_user_relate_reslt2 as &$miss_user_relate_reslt2_one){
                //获取任务详情－mission表
                $mission_field = array('mission_id','mission_name','jobs','duration','salary1','salary2','location1','location2','publish_time','status','valid_time','agent');
                $mission_map2['mission_id'] = array('eq',$miss_user_relate_reslt2_one['mission_id']);
                $mission_db = M('Mission');
                $mission_result2 = $mission_db->field($mission_field)->where($mission_map2)->select();
                if(is_array($mission_result2)){
                    $result2[$i] = $mission_result2[0];
                    $i += 1;
                }else{
                    //获取mission表信息失败
                }

            }

            $result[1] = $result2;
        }else{
            $result[1] = $result2;
        }

        //完成的活
        $miss_user_relate_field = array('mission_id');
        $miss_user_relate_order = '`id` desc';
        $miss_user_relate_map3['follower_id'] = array('eq',$follower_id);
        $miss_user_relate_map3['is_selected'] = array('eq',3);
        $miss_user_relate_reslt3 = $this->field($miss_user_relate_field)->order($miss_user_relate_order)->where($miss_user_relate_map3)->select();
        //dump($miss_user_relate_reslt);
        if(is_array($miss_user_relate_reslt3)){
            $i = 0;
            foreach($miss_user_relate_reslt3 as &$miss_user_relate_reslt3_one){
                //获取任务详情－mission表
                $mission_field = array('mission_id','mission_name','jobs','duration','salary1','salary2','location1','location2','publish_time','status','valid_time','agent');
                $mission_map3['mission_id'] = array('eq',$miss_user_relate_reslt3_one['mission_id']);
                $mission_db = M('Mission');
                $mission_result3 = $mission_db->field($mission_field)->where($mission_map3)->select();
                if(is_array($mission_result3)){
                    $result3[$i] = $mission_result3[0];
                    $i += 1;
                }else{
                    //获取mission表信息失败
                }

            }

            $result[2] = $result3;
        }else{
            $result[2] = $result3;
        }
        //dump($result);
        return $result;
    }

    //
    public function isExist($mission_id,$follower_id){
        //dump('hh');
        $miss_user_relate_map['mission_id'] = array('eq',$mission_id);
        $miss_user_relate_map['follower_id'] = array('eq',$follower_id);
        $miss_user_relate_result = $this->where($miss_user_relate_map)->select();
        //dump($miss_user_relate_result);
        if(is_array($miss_user_relate_result)){
            return true;
        }else{
            return false;
        }
    }

    //获取乙方等级level
    public function getLevelY($user_id){

        $miss_user_relate_map['follower_id'] = array('eq',$user_id) ;
        $miss_user_relate_map['is_selected'] = array('eq',3);
        //$miss_user_relate_map['evaluate'] = array('neq',null);
        $miss_user_relate_result = $this->where($miss_user_relate_map)->count();
        if( 0 <= $miss_user_relate_result && $miss_user_relate_result <= 3 ){
            return 1;
        }elseif( 4 <= $miss_user_relate_result && $miss_user_relate_result <= 15 ){
            return 2;
        }elseif( 16 <= $miss_user_relate_result && $miss_user_relate_result <= 30 ){
            return 3;
        }elseif( 31 <= $miss_user_relate_result && $miss_user_relate_result <= 60 ){
            return 4;
        }elseif( 61 <= $miss_user_relate_result && $miss_user_relate_result <= 100 ){
            return 5;
        }elseif( 101 < $miss_user_relate_result && $miss_user_relate_result <= 150 ){
            return 6;
        }elseif( 151 <= $miss_user_relate_result && $miss_user_relate_result <=250 ){
            return 7;
        }elseif( 251 <= $miss_user_relate_result && $miss_user_relate_result <= 500 ){
            return 8;
        }elseif( 501 <= $miss_user_relate_result ){
            return 9;
        }else{
            return false;
        }

    }

} 